package classes.helper
{
		import flash.events.Event;
		import flash.net.FileFilter;
		import flash.net.FileReference;
		import flash.net.URLRequest;
		import flash.net.URLRequestMethod;
		import flash.net.URLVariables;
		import flash.utils.ByteArray;
		
		import mx.controls.Alert;
		import mx.core.FlexGlobals;
		import mx.messaging.ChannelSet;
		import mx.messaging.config.ServerConfig;
		import mx.rpc.AsyncResponder;
		import mx.rpc.AsyncToken;
		import mx.rpc.events.FaultEvent;
		import mx.rpc.events.ResultEvent;
		import mx.rpc.http.HTTPService;
		import mx.rpc.remoting.RemoteObject;
		
		public class AMFUploader {
			private static var allFilter:FileFilter = new FileFilter('All Files (*.*)', '*.*');
			private static var allTypes:Array = [allFilter];
			
			private var fileReference:FileReference = null;
			private var _serverAddress:String = null;
			private var _description:String = null;
			
			private var fileStreamer:RemoteObject = null;
			
			public function AMFUploader(serverAddress:String) {
				this._serverAddress = serverAddress;
			}
			
			private function browseFileReference():void {
				
				try {
					fileReference = new FileReference();
					fileReference.addEventListener(Event.SELECT, fileSelectedHandler);
					fileReference.addEventListener(Event.COMPLETE, fileCompleteHandler);
					fileReference.addEventListener(Event.CANCEL, fileCancelHandler);
					
					try {
						var success:Boolean = fileReference.browse(allTypes);
					} catch (error:Error) {
						trace(error.message);
					}
				} catch (error:Error) {
					trace(error.message);
				}
			}
			
			public function upload(description:String):void {
				_description = description;
				
				browseFileReference();
			}
			
			private function fileSelectedHandler(event:Event):void {
				fileReference.load();          
			}
			
			private function fileCancelHandler(event:Event):void {
				fileReference = null;
			}
			
			private function fileCompleteHandler(event:Event):void {
				fileReference.removeEventListener(Event.SELECT, fileSelectedHandler);
				fileReference.removeEventListener(Event.COMPLETE, fileCompleteHandler);
				fileReference.removeEventListener(Event.CANCEL, fileCancelHandler);
				
				if (!fileStreamer) {
					fileStreamer = new RemoteObject();
					fileStreamer.destination = "amfUploader";
				}
				var data:ByteArray = new ByteArray();
				fileReference.data.readBytes(data, 0, fileReference.data.length);
				
				var token:AsyncToken = fileStreamer.upload(data, fileReference.name, _description);
				token.data = event;
				token.addResponder(new AsyncResponder(uploadResultHandler, uploadFaultHandler));
				
			}
			
			private function uploadResultHandler(event:ResultEvent, token:AsyncToken):void {
				Alert.show("You can download your file from the link bellow:\n" + _serverAddress + "/" + fileReference.name, "Success");
			}
			
			private function uploadFaultHandler(event:FaultEvent, token:AsyncToken):void {
				trace(event.fault.faultString);
			}
		}
}